<?php
/**
 * Object Relational Persistence Mapping Class for table: test_item_asso_service
 *
 * @package	com.clearhealth
 * @author	ClearHealth Inc.
 */
class TestItemAssoService extends ORDataObject {

	/**#@+
	 * Fields of table: test_item_asso_service mapped to class members
	 */
	var $test_item_asso_service_id		= '';
	var $ticket_item_id		= '';
	var $service_id		= '';
    var $discount = '';
    var $original_price = '';
    var $insurance_price = '';
    var $patient_price ='';	
	/**#@-*/


	/**
	 * DB Table
	 */
	var $_table = 'test_item_asso_service';

	/**
	 * Primary Key
	 */
	var $_key = 'test_item_asso_service_id';
	
	/**
	 * Internal Name
	 */
	var $_internalName = 'TestItemAssoService';

	/**
	 * Handle instantiation
	 */
	function TestItemAssoService() {
		parent::ORDataObject();
	}

	public function getServiceInATest($ticketItemId) {
		$ds = new Datasource_sql();
		$sql = array(
			'cols'	=> "service_id",
			'from'	=> "$this->_table",
			'where'	=> "ticket_item_id = $ticketItemId"			
		);
		$ds->setup($this->_db, $sql);
		return $ds->toArray();
	}
	
	public function delete($ticketItemId) {
		$sql = "delete from $this->_table where ticket_item_id = $ticketItemId";
		$this->_execute($sql);
	}		
	
	public function addItem($ticketItemId, $testItem) {
			
		foreach ($testItem as $serviceId) {
			$data = array (
				'ticket_item_id' 	=> $ticketItemId,
				'service_id'		=> $serviceId);
			$or = Celini::newORDO('TestItemAssoService');
			$or->populateArray($data);			
			$or->persist();
		}
	}
	
	public function listTestService($ticketTestId) {
	    $field  =   L10nHelper::changeField(L10nHelper::getLocale(), 'name');
		$ds = new Datasource_sql();
		$field = L10nHelper::getLocale();
		$sql = array(
			'cols'	=> "S.service_id, S.$field name",
			'from'	=> "test_item_asso_service TI 
				INNER JOIN service S ON TI.service_id = S.service_id",
				
			'where'	=> "TI.ticket_item_id = $ticketTestId",
			'orderby'	=> "name"
		);
		$ds->setup($this->_db, $sql);
		return $ds->toArray();
	}
	
	public function updateItemPrice($ticketItemId, $serviceId, $price){
		$sql = "UPDATE test_item_asso_service 
				SET original_price = ".$price['original_price'].",
					patient_price = ".$price['patient_price'].",
					discount = ".$price['discount'].",
					insurance_price = ".$price['insurance_price']."
				WHERE ticket_item_id = $ticketItemId AND 
					service_id = $serviceId
				";
		$this->_execute($sql);	
	}
}
?>
